package com.laoli.myspringboottest.controller;

import com.laoli.myspringboottest.dto.CreateBDto;
import com.laoli.myspringboottest.service.TableAService;
import com.laoli.myspringboottest.event.TestEvent;
import com.laoli.myspringboottest.model.TestItem;
import com.laoli.myspringboottest.service.TableBService;
import com.laoli.myspringboottest.service.TestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author iamli
 */
@AllArgsConstructor
@RestController
@Slf4j
@RequestMapping("test")
@Api(tags = "测试")
public class TestController {


    private final ApplicationContext publisher;

    private final TableAService tableAService;

    private final TableBService tableBService;

    private final TestService testService;

    @PostMapping("/publisher")
    public String publisher() {
        TestEvent event = new TestEvent(this, new TestItem("tom"));
        log.info("*********************publisher before:" + event.getTestItem().getName());
        publisher.publishEvent(event);
        log.info("*********************publisher after:" + event.getTestItem().getName());
        return "success";
    }

    @PostMapping("/pub")
    public void sPublisher() {
        tableAService.publishEvent();
    }

    @PostMapping("/enum")
    @ApiOperation("枚举参数测试")
    public void enumTest(@Validated @RequestBody CreateBDto dto) {
        tableBService.createEnumTest(dto);

    }

    @ApiOperation("POST:文件下载")
    @PostMapping("/xml/export")
    public void export(HttpServletResponse response) throws Exception {
        testService.createXml(response);
    }

    @ApiOperation("POST:基于jaxb的xml文件下载")
    @PostMapping("/xml/jaxb/export")
    public void exportByJaxb(HttpServletResponse response) throws Exception {
        testService.createXmlByJaxb(response);
    }
}
